package com.ivt.controller.student;

import java.util.Date;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONObject;
import com.ivt.core.ex.IvtException;
import com.ivt.core.util.Constants;
import com.ivt.model.Attendance;
import com.ivt.model.Courses;
import com.ivt.model.Student;
import com.ivt.model.view.Position;
import com.ivt.model.view.Rst;
import com.ivt.service.AttendanceService;
import com.ivt.service.CoursesService;
import com.ivt.utils.Convert;
import com.ivt.utils.enums.Sign;

@Controller("web-student-controller")
@RequestMapping("/student")
public class StudentController {

	@Resource
	private AttendanceService attendanceService;

	@Resource
	private CoursesService coursesService;

	@RequestMapping("/sign/{teacherId}/{courseId}")
	public String sign(@PathVariable Long teacherId,
			@PathVariable Long courseId, ModelMap view) {

		Courses courses = coursesService.getCourses(courseId);

		if (null == courses) {
			throw new IvtException("参数错误");
		}

		if (null == courses.getTeacher()
				|| courses.getTeacher().getId() != teacherId) {
			throw new IvtException("非法教师参数");
		}

		view.addAttribute("courses", Convert.toCoursesView(courses));

		return "student:sign";
	}

	@RequestMapping("/ticket/{action}/{courseId}")
	@ResponseBody
	public Rst ticket(@PathVariable String action, @PathVariable Long courseId,
			Position position, final HttpServletRequest request) {
		Rst rst = new Rst();
		Courses courses = coursesService.getCourses(courseId);

		if (null == courses) {
			throw new IvtException("参数错误");
		}

		HttpSession session = request.getSession();

		Student student = (Student) session
				.getAttribute(Constants.SESSION_STUDENT_KEY);

		Attendance attendance = new Attendance();

		attendance.setCourses(courses);
		attendance.setTeacherId(courses.getTeacher().getId());
		attendance.setCoursesTime(courses.getBeginTime());
		attendance.setStudent(student);
		attendance.setSignTime(new Date());

		if (null != position) {
			attendance.setCoordinate(JSONObject.toJSONString(position));
			attendance.setAddress(position.getAddr());
			if(StringUtils.isBlank(attendance.getAddress())){
				attendance.setAddress(position.getCity());
			}
		}

		if ("in".equals(action)) {
			attendance.setSign(Sign.IN);
		} else if ("out".equals(action)) {
			attendance.setSign(Sign.OUT);
		}

		attendanceService.create(attendance);
		rst.setCode(Constants.SUCCESS);
		if ("in".equals(action)) {
			rst.setMessage("签到成功");
		} else if ("out".equals(action)) {
			rst.setMessage("签退成功");
		}
		
		return rst;
	}

}
